package common.library.activities;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import common.library.R;
import common.library.models.Callback;
import common.library.models.CommonResult;
import common.library.models.StringCallback;
import common.library.utils.AnimationUtil;
import common.library.utils.CameraUtil;
import common.library.utils.LogUtil;
import common.library.utils.PermissionUtil;
import common.library.utils.QrUtil;
import common.library.utils.ThreadUtil;
import common.library.utils.ViewUtil;
import lombok.var;

public class ScanQrActivity extends AppCompatActivity {

    TextureView textureView;
    SurfaceTexture surfaceTexture;
    int cameraCount;
    int cameraIndex;
    Camera camera;

    void previewCamera() {
        try {
            if (camera != null) {
                camera.stopPreview();
                camera.release();
            }

            ////使用后置摄像头
            //for (int cameraIndex = 0; cameraIndex < cameraCount; cameraIndex++) {
            //    Camera.getCameraInfo(cameraIndex, cameraInfo);
            //    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            //        camera = Camera.open(cameraIndex);
            //        break;
            //    }
            //}

            camera = Camera.open(cameraIndex++ % cameraCount);
            camera.setDisplayOrientation(90);
            camera.setPreviewTexture(surfaceTexture);

            var size = CameraUtil.getScreenOutputSize(ScanQrActivity.this, camera);
            var cameraParameters = camera.getParameters();
            cameraParameters.setPreviewSize(size.width, size.height);
            cameraParameters.setPictureSize(size.width, size.height);
            camera.setParameters(cameraParameters);
            camera.startPreview();

            //VariableUtil.ratioExtend(cameraSize, screenSize);
            ViewUtil.setLayout(textureView, size);
        } catch (Exception ex) {
            LogUtil.error(ex);
        }
    }

    public static StringCallback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qr);

        AnimationUtil.playScan(this, R.id.imageViewBar);

        textureView = findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
                ScanQrActivity.this.surfaceTexture = surfaceTexture;
                PermissionUtil.applyCamera(ScanQrActivity.this, new Callback() {
                    @Override
                    public void run() {
                        cameraCount = Camera.getNumberOfCameras();
                        previewCamera();
                    }
                });
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });

        //预览/切换摄像头
        ViewUtil.addClickListener(ScanQrActivity.this, R.id.buttonSwitch, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                previewCamera();
            }
        });

        //分线程3秒扫一次，用主线程会卡死
        final var commonResult = new CommonResult();
        final var task = new Runnable() {
            @Override
            public void run() {
                var bitmap = textureView.getBitmap();
                if (bitmap == null)
                    return;
                var result = QrUtil.superDecode(bitmap);
                if (result != null) {
                    commonResult.success = true;
                    camera.stopPreview();
                    camera.release();
                    finish();
                    callback.run(result);
                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!commonResult.success) {
                    task.run();
                    ThreadUtil.sleep(500);
                }
            }
        }).start();
    }
}
